#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/6/8 11:06
# @Author : linjinting
# @Project : custom_protocol
# @File : message_struct.py
# @Software: PyCharm

from ctypes import *


class JZStruct(Structure):
    def __new__(cls, raw_buffer=None):
        if raw_buffer:
            return cls.from_buffer_copy(raw_buffer)
        return super().__new__(cls)

    def __init__(self, raw_buffer=None, *args, **kw):
        super().__init__(*args, **kw)
        self.convert_initial()

    def convert_initial(self):
        pass

    def data_bytes(self):
        return []
        # return self.IDList + [self.dataType, self.state] + self.timeList + self.msList

    def set_parity(self):
        self.convert_initial()
        self.parity = self.get_parity()

    def get_parity(self):
        list_bytes = self.data_bytes()
        parity = -1
        low = -1
        for x in list_bytes:
            if parity == -1:
                height = low
                low = x
            else:
                height = x
                low = parity
            if height == -1:
                continue
            parity = (height ^ low) ^ 0xff
            # print(parity)
        return parity

    def check_parity(self):
        return self.get_parity() == self.parity

    def size(self):
        return sizeof(self)

    def get_string_at(self):
        return string_at(addressof(self), sizeof(self))


class YXStruct(JZStruct):
    _fields_ = [
        ("headerArray", c_ubyte * 2),
        ("version", c_ubyte),
        ("commandType", c_ubyte),
        ("lengthArray", c_ubyte * 2),
        ("IDArray", c_ubyte * 4),
        ("dataType", c_ubyte),
        ("state", c_ubyte),
        ("timeArray", c_ubyte * 4),
        ("msArray", c_ubyte * 2),
        ("parity", c_ubyte),
    ]

    def convert_initial(self):
        self.header = list(self.headerArray)
        self.lengthList = list(self.lengthArray)
        self.IDList = list(self.IDArray)
        self.timeList = list(self.timeArray)
        self.msList = list(self.msArray)
        self.length = (self.lengthList[1] << 8) + self.lengthList[0]
        self.ID = (self.IDList[3] << 24) + (self.IDList[2] << 16) + (self.IDList[1] << 8) + self.IDList[0]
        self.time = (self.timeList[3] << 24) + (self.timeList[2] << 16) + (self.timeList[1] << 8) + self.timeList[0]
        self.ms = (self.msList[1] << 8) + self.msList[0]

    def data_bytes(self):
        return self.IDList + [self.dataType, self.state] + self.timeList + self.msList


class YKStruct(JZStruct):
    _fields_ = [
        ("headerArray", c_ubyte * 2),
        ("version", c_ubyte),
        ("commandType", c_ubyte),
        ("lengthArray", c_ubyte * 2),
        ("IDArray", c_ubyte * 4),
        ("dataType", c_ubyte),
        ("state", c_ubyte),
        ("timeArray", c_ubyte * 4),
        ("parity", c_ubyte),
    ]

    def convert_initial(self):
        self.header = list(self.headerArray)
        self.lengthList = list(self.lengthArray)
        self.IDList = list(self.IDArray)
        self.timeList = list(self.timeArray)

        self.length = (self.lengthList[1] << 8) + self.lengthList[0]
        self.ID = (self.IDList[3] << 24) + (self.IDList[2] << 16) + (self.IDList[1] << 8) + self.IDList[0]
        self.time = (self.timeList[3] << 24) + (self.timeList[2] << 16) + (self.timeList[1] << 8) + self.timeList[0]

    def data_bytes(self):
        return self.IDList + [self.dataType, self.state] + self.timeList


def split_int_to_simple_byte(bytes_array, int_num, i=3):
    offset = 8 * i

    simple_byte_int = int_num >> offset
    bytes_array.append(simple_byte_int)
    residue_byte = int_num - (simple_byte_int << offset)
    i -= 1
    if i == 0:
        bytes_array.append(residue_byte)
        return bytes_array
    return split_int_to_simple_byte(bytes_array, residue_byte, i)


if __name__ == '__main__':
    b = b'\xeb\x90\x01\x04\r\x00\xa3\xfe\x1c\r\x04\x01|\xb0GY\x01\x00e'
    BS = 'eb900001023'

    # new_test = " ".join(text_list)
    # print(new_test)
    # t1 = JZ_Struct(b)
    # t2 = JZ_Struct()
    # print(t1, t2)
    # print(t1.header)
    # array = c_ubyte * 2
    # t2.header = array(0xeb, 0x90)
    # print(list(t2.header))
